********************************************************************************
*
*               GENDER COMPOSITION AND PERFORMANCE, Tables 1 and 2
*
********************************************************************************

********** Gender composition of subjects, Table 1


clear
use "${data}cleaned_data_y3.dta"
do "${blocks}construct_new_variables"


tempname statistics
tempfile stats
postfile `statistics' ///
    str100 statistic count share se using "`stats'", replace

	gen female=1 if Gender=="Female"
	replace female=0 if Gender=="Male"

	
	ci means female
	local share = r(mean)*100
	local count = r(N)
	local se = r(se)*100

	post `statistics' ("\textit{All subjects}")  (`count') (`share') (`se')
	
	replace Course_NatSci_split_str="Biological Sciences" if Course_NatSci_split_str=="Biological NatSci"
	replace Course_NatSci_split_str="Physical Sciences" if Course_NatSci_split_str=="Physical NatSci"

	local course_EEMP `""Economics" "Engineering" "Mathematics" "Physical Sciences""'
	local course_nEEMP `" "Law"  "Medicine" "Biological Sciences" "'
	
	ci means female if EEMP==1
	local share = r(mean)*100
	local count = r(N)
	local se = r(se)*100
	
	post `statistics' ("")  (.) (.) (.) 

	post `statistics' ("\textit{MI subjects}")  (`count') (`share') (`se') 

	foreach c of local course_EEMP {
		
	ci means female if Course_NatSci_split_str=="`c'"
	local share=r(mean)*100
	local count=r(N)
	local se = r(se)*100

	
	post `statistics' ("\quad `c'") (`count') (`share') (`se') 
	}

	ci means female if EEMP==0
	local share = r(mean)*100
	local count = r(N)
	local se = r(se)*100

	post `statistics' ("")  (.) (.) (.) 
		
	post `statistics' ("\textit{Non-MI subjects}")  (`count') (`share') (`se') 

	foreach c of local course_nEEMP {
		
	ci means female if Course_NatSci_split_str=="`c'"
	local share=r(mean)*100
	local count=r(N)
	local se = r(se)*100
		
	post `statistics' ("\quad `c'") (`count') (`share') (`se')
	}

	
postclose `statistics'	

use `stats', clear
list

format share %9.1fc
format se %9.1fc
list

listtab * using "${tables}descriptives_0.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c c c  D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
    " & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Percent female} & \multicolumn{1}{c}{SE} \\  \midrule" "& (1) & (2) & (3)  \\ \midrule") ///
    foot("\bottomrule" "\end{tabular}")

	
********** Gender, offer probabilities and performance, Table 2

clear
use "${data}cleaned_data_y3.dta"
do "${blocks}construct_new_variables"

egen ns_mean_perc_y3 = mean(Percentage_y3), by(Course_NatSci_split)
egen ns_sd_perc_y3   = sd(Percentage_y3), by(Course_NatSci_split)
gen ns_std_perc_y3   = (Percentage_y3 - ns_mean_perc_y3) / ns_sd_perc_y3

tempname gender_perf
tempfile gp
postfile `gender_perf' ///
    str100 statistic count share foffer moffer y1diff y3diff using "`gp'", replace

	gen female=1 if Gender=="Female"
	replace female=0 if Gender=="Male"

	su female 
	local share = r(mean)*100
	local count = r(N)
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1, by(female)
	local y1diff=r(mu_2)-r(mu_1)

	ttest  ns_std_perc_y3, by(female)
	local y3diff=r(mu_2)-r(mu_1)

	post `gender_perf' ("All subjects")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	ci mean female 
	local share = r(se)*100
	local count = .
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1, by(female)
	local y1diff=r(se)

	ttest  ns_std_perc_y3, by(female)
	local y3diff=r(se)

	post `gender_perf' ("\quad SE All")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	***
	
	replace Course_NatSci_split_str="Biological Sciences" if Course_NatSci_split_str=="Biological NatSci"
	replace Course_NatSci_split_str="Physical Sciences" if Course_NatSci_split_str=="Physical NatSci"

	local course_EEMP `""Economics" "Engineering" "Mathematics" "Physical Sciences""'	
	local course_nEEMP `" "Law"  "Medicine" "Biological Sciences" "'

	
	su female if EEMP==1
	local share = r(mean)*100
	local count = r(N)
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1  if EEMP==1, by(female)
	local y1diff=r(mu_2)-r(mu_1)

	ttest  ns_std_perc_y3   if EEMP==1, by(female)
	local y3diff=r(mu_2)-r(mu_1)

	post `gender_perf' ("")  (.) (.) (.) (.) (.) (.)

	post `gender_perf' ("MI subjects")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	ci mean female if EEMP==1
	local share = r(se)*100
	local count = .
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1  if EEMP==1, by(female)
	local y1diff=r(se)

	ttest  ns_std_perc_y3  if EEMP==1, by(female)
	local y3diff=r(se)

	post `gender_perf' ("\quad SE MI")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	
	su female if EEMP==0
	local share = r(mean)*100
	local count = r(N)
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1 if EEM==0, by(female)
	local y1diff=r(mu_2)-r(mu_1)

	ttest  ns_std_perc_y3   if EEM==0, by(female)
	local y3diff=r(mu_2)-r(mu_1)


	post `gender_perf' ("")  (.) (.) (.) (.) (.) (.)

	post `gender_perf' ("Non-MI subjects")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	ci mean female if EEMP==0
	local share = r(se)*100
	local count = .
		
	local foffer =.
	local moffer =.
	
	ttest  ns_std_perc_y1  if EEMP==0, by(female)
	local y1diff=r(se)

	ttest  ns_std_perc_y3  if EEMP==0, by(female)
	local y3diff=r(se)

	post `gender_perf' ("\quad SE Non-MI")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

********** Offer rates
	
	clear
	use "${data}merged_tripos.dta"

	*** Prep data
	keep if Course != "PBS"
	keep if Course != "Other"
	count
	
	keep if ApplyYear<=2016
	
	replace Offers = 0 if Offers != 1

	*** Statistics
	
	local count=.
	local share=.
	
	su Offers if Gender == "Female"
	local foffer = r(mean)*100
	
	su Offers if Gender == "Male"
	local moffer = r(mean)*100
	
	local y1diff=.
	local y3diff=.
		
	post `gender_perf' ("All subjects")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	local count=.
	local share=.
	
	ci mean Offers if Gender == "Female"
	local foffer = r(se)*100
	
	ci mean Offers if Gender == "Male"
	local moffer = r(se)*100
	
	local y1diff=.
	local y3diff=.	
	
	post `gender_perf' ("\quad SE All")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	local count=.
	local share=.
	
	su Offers if Gender == "Female" & (Course == "Economics" | Course == "Engineering"  | Course == "Mathematics")
	local foffer = r(mean)*100
	
	su Offers if Gender == "Male" & (Course == "Economics" | Course == "Engineering"  | Course == "Mathematics")
	local moffer = r(mean)*100
	
	local y1diff=.
	local y3diff=.
	
	post `gender_perf' ("MI subjects") (`count') (`share') (`foffer') (`moffer')  (`y1diff') (`y3diff')

	local count=.
	local share=.
	
	ci mean Offers if Gender == "Female" & (Course == "Economics" | Course == "Engineering"  | Course == "Mathematics")
	local foffer = r(se)*100
	
	ci mean Offers if Gender == "Male" & (Course == "Economics" | Course == "Engineering"  | Course == "Mathematics")
	local moffer = r(se)*100
	
	local y1diff=.
	local y3diff=.	
	
	post `gender_perf' ("\quad SE MI")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')


	local count=.
	local share=.
	
	su Offers if Gender == "Female" & (Course == "Law" | Course == "Medicine")
	local foffer = r(mean)*100
	
	su Offers if Gender == "Male" & (Course == "Law" | Course == "Medicine")
	local moffer = r(mean)*100
	
	local y1diff=.
	local y3diff=.
	
	post `gender_perf' ("Non-MI subjects") (`count') (`share') (`foffer') (`moffer')  (`y1diff') (`y3diff')

	
	local count=.
	local share=.
	
	ci mean Offers if Gender == "Female"  & (Course == "Law" | Course == "Medicine")
	local foffer = r(se)*100
	
	ci mean Offers if Gender == "Male"  & (Course == "Law" | Course == "Medicine")
	local moffer = r(se)*100
	
	local y1diff=.
	local y3diff=.	
	
	post `gender_perf' ("\quad SE Non-MI")  (`count') (`share') (`foffer') (`moffer') (`y1diff') (`y3diff')

	
postclose `gender_perf'	
use `gp', clear
format share %9.1fc
format moffer %9.1fc
format foffer %9.1fc
*replace moffer=round(moffer,0.1)
*replace foffer=round(foffer,0.1)
format *diff  %9.2fc
list

gen id=_n

foreach x in f m {
sort statistic `x'offer 
by statistic: replace `x'offer=`x'offer[_n-1] if `x'offer==. & `x'offer[_n-1]~=. 
*tostring `x'offer, replace force
*replace `x'offer="" if `x'offer=="."
}

drop if share==. & foffer~=.
sort id
drop id

listtab * using "${tables}descriptives_1.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c c c c c c D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
    " & \multicolumn{1}{c}{\multirow{2}{*}{N}} & \multicolumn{1}{c}{\multirow{2}{*}{Percent female}} & \multicolumn{2}{c}{Offer probability} & \multicolumn{2}{c}{Female score - Male score} \\" ///
	"\cmidrule(lr){4-5}\cmidrule(lr){6-7}" ///
	"&  &  & \multicolumn{1}{c}{Females} & \multicolumn{1}{c}{Males} & \multicolumn{1}{c}{Year 1} & \multicolumn{1}{c}{Year 3} \\  \midrule" "& (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule") ///
    foot("\bottomrule" "\end{tabular}")
